home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Linux / Kubuntu 8.10 / kubuntu-8.10-desktop-i386.iso / casper / filesystem.squashfs / usr / share / pycentral-data / pyversions.py next >
Text File  |  2008-05-28  |  15KB  |  398 lines

  1. #! /usr/bin/python
  2.  
  3. import os, re, string, sys
  4. try:
  5.     SetType = set
  6. except NameError:
  7.     import sets
  8.     SetType = sets.Set
  9.     set = sets.Set
  10.  
  11. class PyCentralEmptyValueError(ValueError):
  12.     """Python Central Value Error"""
  13.     pass
  14.  
  15. _defaults = None
  16. def read_default(name=None):
  17.     global _defaults
  18.     from ConfigParser import SafeConfigParser, NoOptionError
  19.     if not _defaults:
  20.         if os.path.exists('/usr/share/python/debian_defaults'):
  21.             config = SafeConfigParser()
  22.             config.readfp(file('/usr/share/python/debian_defaults'))
  23.             _defaults = config
  24.         else:
  25.             raise ValueError
  26.     if _defaults and name:
  27.         try:
  28.             value = _defaults.get('DEFAULT', name)
  29.         except NoOptionError:
  30.             raise ValueError
  31.         return value
  32.     return None
  33.  
  34. def parse_versions(vstring):
  35.     # Fix packages which were built with the broken dh_pycentral 0.5.50ubuntu1.
  36.     if vstring.lower().startswith("python:versions="):
  37.         vstring = string.split(vstring, "=", maxsplit=1)[1]
  38.     import operator
  39.     operators = { None: operator.eq, '=': operator.eq,
  40.                   '>=': operator.ge, '<=': operator.le,
  41.                   '<<': operator.lt
  42.                   }
  43.     vinfo = {}
  44.     exact_versions = set([])
  45.     version_range = set(supported_versions(version_only=True)
  46.                         + old_versions(version_only=True))
  47.     relop_seen = False
  48.     for field in vstring.split(','):
  49.         field = field.strip()
  50.         if field == 'all':
  51.             vinfo['all'] = 'all'
  52.             continue
  53.         if field in ('current', 'current_ext'):
  54.             vinfo['current'] = field
  55.             continue
  56.         vinfo.setdefault('versions', set())
  57.         ve = re.compile('(>=|<=|<<|=)? *(\d\.\d)$')
  58.         m = ve.match(field)
  59.         try:
  60.             op, v = m.group(1), m.group(2)
  61.             if op in (None, '='):
  62.                 exact_versions.add(v)
  63.             else:
  64.                 relop_seen = True
  65.                 filtop = operators[op]
  66.                 version_range = [av for av in version_range if filtop(av ,v)]
  67.         except Exception:
  68.             raise ValueError, 'error parsing Python-Version attribute'
  69.     if 'versions' in vinfo:
  70.         vinfo['versions'] = exact_versions
  71.         if relop_seen:
  72.             vinfo['versions'] = exact_versions.union(version_range)
  73.     return vinfo
  74.  
  75. _old_versions = None
  76. def old_versions(version_only=False):
  77.     global _old_versions
  78.     if not _old_versions:
  79.         try:
  80.             value = read_default('old-versions')
  81.             _old_versions = [s.strip() for s in value.split(',')]
  82.         except ValueError:
  83.             _old_versions = []
  84.     if version_only:
  85.         return [v[6:] for v in _old_versions]
  86.     else:
  87.         return _old_versions
  88.  
  89. _unsupported_versions = None
  90. def unsupported_versions(version_only=False):
  91.     global _unsupported_versions
  92.     if not _unsupported_versions:
  93.         try:
  94.             value = read_default('unsupported-versions')
  95.             _unsupported_versions = [s.strip() for s in value.split(',')]
  96.         except ValueError:
  97.             _unsupported_versions = []
  98.     if version_only:
  99.         return [v[6:] for v in _unsupported_versions]
  100.     else:
  101.         return _unsupported_versions
  102.  
  103. _supported_versions = None
  104. def supported_versions(version_only=False):
  105.     global _supported_versions
  106.     if not _supported_versions:
  107.         try:
  108.             value = read_default('supported-versions')
  109.             _supported_versions = [s.strip() for s in value.split(',')]
  110.         except ValueError:
  111.             cmd = ['/usr/bin/apt-cache', '--no-all-versions',
  112.                    'show', 'python-all']
  113.             try:
  114.                 import subprocess
  115.                 p = subprocess.Popen(cmd, bufsize=1,
  116.                                      shell=False, stdout=subprocess.PIPE)
  117.                 fd = p.stdout
  118.             except ImportError:
  119.                 fd = os.popen(' '.join(cmd))
  120.             depends = None
  121.             for line in fd:
  122.                 if line.startswith('Depends:'):
  123.                     depends = line.split(':', 1)[1].strip().split(',')
  124.             fd.close()
  125.             if depends:
  126.                 depends = [re.sub(r'\s*(\S+)[ (]?.*', r'\1', s) for s in depends]
  127.                 _supported_versions = depends
  128.             if not _supported_versions:
  129.                 # last resort: python-minimal not installed, apt-cache
  130.                 # not available, hard code the value, #394084
  131.                 _supported_versions = ['python2.4', 'python2.5']
  132.     if version_only:
  133.         return [v[6:] for v in _supported_versions]
  134.     else:
  135.         return _supported_versions
  136.  
  137. _default_version = None
  138. def default_version(version_only=False):
  139.     global _default_version
  140.     if not _default_version:
  141.         try:
  142.             _default_version = link = os.readlink('/usr/bin/python')
  143.         except OSError:
  144.             _default_version = None
  145.             try:
  146.                 cmd = ['/usr/bin/python', '-c', 'import sys; print sys.version[:3]']
  147.                 import subprocess
  148.                 p = subprocess.Popen(cmd, bufsize=1,
  149.                                      shell=False, stdout=subprocess.PIPE)
  150.                 fd = p.stdout
  151.             except ImportError:
  152.                 fd = os.popen("/usr/bin/python -c 'import sys; print sys.version[:3]'")
  153.             line = fd.readline().strip()
  154.             fd.close()
  155.             if re.match(r'\d\.\d$', line):
  156.                 _default_version = 'python' + line
  157.         # consistency check
  158.         try:
  159.             debian_default = read_default('default-version')
  160.         except ValueError:
  161.             debian_default = "python2.4"
  162.         if not _default_version in (debian_default, os.path.join('/usr/bin', debian_default)):
  163.             raise ValueError, "/usr/bin/python does not match the python default version. It must be reset to point to %s" % debian_default
  164.         _default_version = debian_default
  165.     if version_only:
  166.         return _default_version[6:]
  167.     else:
  168.         return _default_version
  169.  
  170. def requested_versions(vstring, version_only=False):
  171.     versions = None
  172.     vinfo = parse_versions(vstring)
  173.     supported = supported_versions(version_only=True)
  174.     if len(vinfo) == 1:
  175.         if 'all' in vinfo:
  176.             versions = supported
  177.         elif 'current' in vinfo:
  178.             versions = [default_version(version_only=True)]
  179.         else:
  180.             versions = vinfo['versions'].intersection(supported)
  181.     elif 'all' in vinfo and 'current' in vinfo:
  182.         raise ValueError, "both `current' and `all' in version string"
  183.     elif 'all' in vinfo:
  184.         versions = versions = vinfo['versions'].intersection(supported)
  185.     elif 'current' in vinfo:
  186.         current = default_version(version_only=True)
  187.         if not current in vinfo['versions']:
  188.             raise ValueError, "`current' version not in supported versions"
  189.         versions = [current]
  190.     else:
  191.         raise ValueError, 'error in version string'
  192.     if not versions:
  193.         raise PyCentralEmptyValueError, 'empty set of versions'
  194.     if version_only:
  195.         return versions
  196.     else:
  197.         return ['python%s' % v for v in versions]
  198.  
  199. # This function is used by python-central to decide which installed
  200. # runtimes must be supported. It's not nice, but it's designed to mimic
  201. # closely requested_versions in an attempt to avoid introducing bugs this
  202. # late in the release cycle. Some cleanup is in order post-etch though.
  203. def requested_versions_for_runtime(vstring, version_only=False):
  204.     versions = None
  205.     vinfo = parse_versions(vstring)
  206.     old = old_versions(version_only=True)
  207.     unsupported = unsupported_versions(version_only=True)
  208.     supported = supported_versions(version_only=True)
  209.     # You might want to add unsupported versions too... later.
  210.     supported.extend(old)
  211.     if len(vinfo) == 1:
  212.         if 'all' in vinfo:
  213.             versions = supported
  214.         elif 'current' in vinfo:
  215.             versions = [default_version(version_only=True)]
  216.         else:
  217.             versions = vinfo['versions'].intersection(supported)
  218.     elif 'all' in vinfo and 'current' in vinfo:
  219.         raise ValueError, "both `current' and `all' in version string"
  220.     elif 'all' in vinfo:
  221.         versions = versions = vinfo['versions'].intersection(supported)
  222.     elif 'current' in vinfo:
  223.         current = default_version(version_only=True)
  224.         if not current in vinfo['versions']:
  225.             raise ValueError, "`current' version not in supported versions"
  226.         versions = [current]
  227.     else:
  228.         raise ValueError, 'error in version string'
  229.     if not versions:
  230.         raise PyCentralEmptyValueError, 'empty set of versions'
  231.     if version_only:
  232.         return versions
  233.     else:
  234.         return ['python%s' % v for v in versions]
  235.  
  236. def installed_versions(version_only=False):
  237.     import glob
  238.     supported = supported_versions()
  239.     versions = [os.path.basename(s)
  240.                 for s in glob.glob('/usr/bin/python[0-9].[0-9]')
  241.                 if os.path.basename(s) in supported]
  242.     versions.sort()
  243.     if version_only:
  244.         return [v[6:] for v in versions]
  245.     else:
  246.         return versions
  247.  
  248. class ControlFileValueError(ValueError):
  249.     pass
  250. class MissingVersionValueError(ValueError):
  251.     pass
  252.  
  253. def extract_pyversion_attribute(fn, pkg):
  254.     """read the debian/control file, extract the XS-Python-Version
  255.     field; check that XB-Python-Version exists for the package."""
  256.  
  257.     version = None
  258.     sversion = None
  259.     section = None
  260.     for line in file(fn):
  261.         line = line.strip()
  262.         if line == '':
  263.             if pkg == 'Source':
  264.                 break
  265.             section = None
  266.         elif line.startswith('Source:'):
  267.             section = 'Source'
  268.         elif line.startswith('Package: ' + pkg):
  269.             section = pkg
  270.         elif line.startswith('XS-Python-Version:'):
  271.             if section != 'Source':
  272.                 raise ValueError, \
  273.                       'attribute XS-Python-Version not in Source section'
  274.             sversion = line.split(':', 1)[1].strip()
  275.         elif line.startswith('XB-Python-Version:'):
  276.             if section == pkg:
  277.                 version = line.split(':', 1)[1].strip()
  278.     if section == None:
  279.         raise ControlFileValueError, 'not a control file'
  280.     if pkg == 'Source':
  281.         if sversion == None:
  282.             raise MissingVersionValueError, \
  283.                   'missing XS-Python-Version in control file'
  284.         return sversion
  285.     if version == None:
  286.         raise MissingVersionValueError, \
  287.               'missing XB-Python-Version for package `%s' % pkg
  288.     return version
  289.  
  290. # compatibility functions to parse debian/pyversions
  291.  
  292. def version_cmp(ver1,ver2):
  293.     v1=[int(i) for i in ver1.split('.')]
  294.     v2=[int(i) for i in ver2.split('.')]
  295.     return cmp(v1,v2)
  296.  
  297. def requested_versions_bis(vstring, version_only=False):
  298.     versions = []
  299.     py_supported_short = supported_versions(version_only=True)
  300.     for item in vstring.split(','):
  301.         v=item.split('-')
  302.         if len(v)>1:
  303.             if not v[0]:
  304.                 v[0] = py_supported_short[0]
  305.             if not v[1]:
  306.                 v[1] = py_supported_short[-1]
  307.             for ver in py_supported_short:
  308.                 try:
  309.                     if version_cmp(ver,v[0]) >= 0 \
  310.                            and version_cmp(ver,v[1]) <= 0:
  311.                         versions.append(ver)
  312.                 except ValueError:
  313.                     pass
  314.         else:
  315.             if v[0] in py_supported_short:
  316.                 versions.append(v[0])
  317.     versions.sort(version_cmp)
  318.     if not versions:
  319.         raise ValueError, 'empty set of versions'
  320.     if not version_only:
  321.         versions=['python'+i for i in versions]
  322.     return versions
  323.  
  324. def extract_pyversion_attribute_bis(fn):
  325.     vstring = file(fn).readline().rstrip('\n')
  326.     return vstring
  327.  
  328. def main():
  329.     from optparse import OptionParser
  330.     usage = '[-v] [-h] [-d|--default] [-s|--supported] [-i|--installed] [-r|--requested <version string>|<control file>]'
  331.     parser = OptionParser(usage=usage)
  332.     parser.add_option('-d', '--default',
  333.                       help='print the default python version',
  334.                       action='store_true', dest='default')
  335.     parser.add_option('-s', '--supported',
  336.                       help='print the supported python versions',
  337.                       action='store_true', dest='supported')
  338.     parser.add_option('-r', '--requested',
  339.                       help='print the python versions requested by a build; the argument is either the name of a control file or the value of the XS-Python-Version attribute',
  340.                       action='store_true', dest='requested')
  341.     parser.add_option('-i', '--installed',
  342.                       help='print the installed supported python versions',
  343.                       action='store_true', dest='installed')
  344.     parser.add_option('-v', '--version',
  345.                       help='print just the version number(s)',
  346.                       default=False, action='store_true', dest='version_only')
  347.     opts, args = parser.parse_args()
  348.     program = os.path.basename(sys.argv[0])
  349.  
  350.     if opts.default and len(args) == 0:
  351.         try:
  352.             print default_version(opts.version_only)
  353.         except ValueError, msg:
  354.             print "%s:" % program, msg
  355.             sys.exit(1)
  356.     elif opts.supported and len(args) == 0:
  357.         print ' '.join(supported_versions(opts.version_only))
  358.     elif opts.installed and len(args) == 0:
  359.         print ' '.join(installed_versions(opts.version_only))
  360.     elif opts.requested and len(args) <= 1:
  361.         if len(args) == 0:
  362.             versions = 'debian/control'
  363.         else:
  364.             versions = args[0]
  365.         try:
  366.             if os.path.isfile(versions):
  367.                 fn = versions
  368.                 try:
  369.                     vstring = extract_pyversion_attribute(fn, 'Source')
  370.                     vs = requested_versions(vstring, opts.version_only)
  371.                 except ControlFileValueError:
  372.                     sys.stderr.write("%s: not a control file: %s, " \
  373.                                      % (program, fn))
  374.                     sys.exit(1)
  375.                 except MissingVersionValueError:
  376.                     fn = os.path.join(os.path.dirname(fn), 'pyversions')
  377.                     sys.stderr.write("%s: missing XS-Python-Version in control file, fall back to %s\n" \
  378.                                      % (program, fn))
  379.                     try:
  380.                         vstring = extract_pyversion_attribute_bis(fn)
  381.                         vs = requested_versions_bis(vstring, opts.version_only)
  382.                     except IOError:
  383.                         sys.stderr.write("%s: missing debian/pyversions file, fall back to supported versions\n" \
  384.                                          % program)
  385.                         vs = supported_versions(opts.version_only)
  386.             else:
  387.                 vs = requested_versions(versions, opts.version_only)
  388.             print ' '.join(vs)
  389.         except ValueError, msg:
  390.             sys.stderr.write("%s: %s\n" % (program, msg))
  391.             sys.exit(1)
  392.     else:
  393.         sys.stderr.write("usage: %s %s\n" % (program, usage))
  394.         sys.exit(1)
  395.  
  396. if __name__ == '__main__':
  397.     main()
  398.